#ifndef KUN_URP_OUTLINE_PASS_INCLUDED
#define KUN_URP_OUTLINE_PASS_INCLUDED

#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"


struct Attributes
{
    float4 positionOS   : POSITION;
    float2 texcoord     : TEXCOORD0;
    float3 normalOS     : NORMAL;
    float4 tangentOS    : TANGENT;
    float2 texcoord0    : TEXCOORD0;
    float2 texcoord7    : TEXCOORD7;
    float4 color        : COLOR0;
    UNITY_VERTEX_INPUT_INSTANCE_ID
};

struct Varyings
{
    float4 positionCS               : SV_POSITION;
    float2 uv                       : TEXCOORD0;
    UNITY_VERTEX_INPUT_INSTANCE_ID
    UNITY_VERTEX_OUTPUT_STEREO
};


Varyings OutlinePassVertex(Attributes input)
{
    // #ifndef _ENABLE_OUTLINE_ON
    //     return (Varyings)0;
    // #endif

    Varyings output = (Varyings)0;

    UNITY_SETUP_INSTANCE_ID(input);
    UNITY_TRANSFER_INSTANCE_ID(input, output);
    UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);

    float4 positionCS = mul(UNITY_MATRIX_MVP, input.positionOS); 
	float3 normalVS = mul((float3x3)UNITY_MATRIX_IT_MV, input.normalOS);  
    float2 normalCS = normalize(mul((float2x2)UNITY_MATRIX_P, normalVS.xy));
    positionCS.xy += _OutlineWidth * normalCS;
	output.positionCS = positionCS;

    // output.uv = input.texcoord0;
    // float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
    // float3 tangentWS = normalize(mul(UNITY_MATRIX_M, float4(input.tangentOS.xyz, 0.0)).xyz);
    // float3 bitangentWS  =normalize(cross(normalWS, tangentWS) * input.tangentOS.w);
    // float3x3 tangentTransform = float3x3(tangentWS, bitangentWS, normalWS);

    // float3 bakedNormalWS = float3(input.texcoord7, 0);
    // bakedNormalWS.z = sqrt(1.0 - saturate(dot(bakedNormalWS.xy, bakedNormalWS.xy)));
    // bakedNormalWS = mul(bakedNormalWS, tangentTransform);

    // float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
    // float3 viewDirWS = normalize(_WorldSpaceCameraPos.xyz - positionWS);
    // float distance = length(viewDirWS);

    // output.positionCS = TransformWorldToHClip(positionWS);
    // output.positionCS.xy += normalize(mul((float3x3)UNITY_MATRIX_VP, bakedNormalWS).xy) * _OutlineWidth;

    return output;
}



half4 OutlinePassFragment(Varyings input) : SV_Target
{
    #ifndef _ENABLE_OUTLINE_ON
        return (half4)0;
    #endif
    UNITY_SETUP_INSTANCE_ID(input);
    UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    return _OutlineColor;
}




#endif